package 每日一题;

public class 机器人的运动范围 {
    int res=0;
    public int movingCount(int m, int n, int k) {
        boolean[][] visited=new boolean[m][n];

        dfs(m,n,0,0,k,visited);
        return res;
    }

    void dfs(int m,int n,int i,int j,int k,boolean[][] visited){
        if(i<0||j<0||i>=m||j>=n){
            return;
        }
        if((i/10+i%10+j/10+j%10)>k) return;
        if(visited[i][j]) return;
        res++;
        visited[i][j]=true;
        dfs(m,n,i-1,j,k,visited);
        dfs(m,n,i+1,j,k,visited);
        dfs(m,n,i,j-1,k,visited);
        dfs(m,n,i,j+1,k,visited);
    }
}
